****************************************************************************
* Purpose: Create background characteristics of parents 
****************************************************************************
do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

********************************************************************
* Create appended file for moms and dads
********************************************************************
forvalues i = 2008/2021 {
use "$raw\GRUNDVIVE`i'" 
gen year = `i'
drop MOR_ID
duplicates drop
rename pnr cprm
keep cprm civst FOED_DAG hustype statsb antboernf IE_TYPE hfaudd LOENMV_13 ERHVERVSINDK_13 FAMILIE_ID kom
foreach var in civst FOED_DAG hustype statsb antboernf IE_TYPE hfaudd LOENMV_13 ERHVERVSINDK_13 FAMILIE_ID kom{
rename `var' `var'_m    
}
duplicates drop 
save "$work\m_`i'", replace
}
forvalues i = 2008/2021 {
use "$raw\GRUNDVIVE`i'" 
gen year = `i'
drop FAR_ID
duplicates drop
rename pnr cprf
keep cprf civst FOED_DAG hustype statsb antboernf IE_TYPE hfaudd LOENMV_13 ERHVERVSINDK_13 FAMILIE_ID kom
foreach var in civst FOED_DAG hustype statsb antboernf IE_TYPE hfaudd LOENMV_13 ERHVERVSINDK_13 FAMILIE_ID kom {
rename `var' `var'_f    
}
duplicates drop 
save "$work\f_`i'", replace
}


******************************************************************
* Merge to children always for the year prior to yob so 2011-2016
******************************************************************

forvalues i = 2011/2016 {
use "$work\mfr_sample", clear // the most general MFR sample - for kids born 2012 and after
keep if mfr_yob == `i' + 1
keep pnr id_mother id_father mfr_yob 
rename id_mother cprm
rename  id_father cprf
merge m:1 cprm using "$work\m_`i'", keep(1 3) nogen
drop if pnr == ""

merge m:1 cprf using "$work\f_`i'", keep(1 3) nogen
drop if pnr == ""
save "$work\background`i'", replace
}
* Append, recode and label
use "$work\background2011", clear
forvalues i = 2012/2016 {
append using "$work\background`i'", force    
}

// Parents' age
gen yob_m = year(FOED_DAG_m)
gen yob_f = year(FOED_DAG_f)
gen alder_m = mfr_yob - yob_m 
gen alder_f = mfr_yob - yob_f 

// Marital status and cohabitation
gen marry_m = 0
replace marry_m = 1 if civst_m == "G"
replace marry_m = . if civst_m == ""
gen marry_f = 0
replace marry_f = 1 if civst_f == "G"
replace marry_f = . if civst_f == ""
gen cohab = 0
replace cohab = 1 if FAMILIE_ID_m == FAMILIE_ID_f
replace cohab = . if cprf == ""
replace cohab = . if cprm == ""

// Income
gen inc_f = LOENMV_13_f
gen inc_m = LOENMV_13_m
drop LOENMV_13_f LOENMV_13_m

// Education
rename hfaudd_m audd
merge m:1 audd using "$work\uddan_2015_audd", keepusing(u1 u1tekst h1 h1tekst  ) nogen keep(1 3)
rename (u1 u1tekst h1 h1tekst audd) (u1sund_m u1tekstsund_m h1sund_m h1tekstsund_m  auddsund_m)
rename hfaudd_f audd
merge m:1 audd using "$work\uddan_2015_audd", keepusing(u1 u1tekst h1 h1tekst  ) nogen keep(1 3)
rename (u1 u1tekst h1 h1tekst  audd) (u1sund_f u1tekstsund_f h1sund_f h1tekstsund_f auddsund_f)
destring h1sund_m, replace
destring h1sund_f, replace
foreach var in m f {
destring h1sund_`var', replace
gen grund_`var' = 0
replace grund_`var' = 1 if h1sund_`var' == 10
replace grund_`var' = . if h1sund_`var' == .
gen higher_`var' = 0
replace higher_`var' = 1 if h1sund_`var' >= 40 & h1sund_`var' < 60
replace higher_`var' = . if h1sund_`var' == .
gen uni_`var' = 0
replace uni_`var' = 1 if h1sund_`var' >= 60 & h1sund_`var' < 71
replace uni_`var' = . if h1sund_`var' == .
drop u1sund_`var' u1tekstsund_`var' h1sund_`var' h1tekstsund_`var' auddsund_`var'

//Ethnicity
gen dk_`var' = 0
replace dk_`var' = 1 if IE_TYPE_`var' == 1 
replace dk_`var' = . if cpr`var' == ""

//Employment status
gen work_`var' = .
replace work_`var' = 1 if ERHVERVSINDK_13_`var' != 0
replace work_`var' = 0 if ERHVERVSINDK_13_`var' == 0
replace work_`var' = . if cpr`var' == ""
}

//Define married as married or cohabiting
gen married = .
replace married = 0 if cohab == 0
replace married = 1 if cohab == 1 & marry_m == 1 & marry_f == 1
replace married = 0 if cohab == 1 & marry_m == 0

//Keep relevant variables
keep pnr cpr* marry* inc* grund* higher* uni* dk* work* married cohab alder* FOED_DAG*
order pnr cpr* marry* inc* grund* higher* uni* dk* work* alder* married cohab FOED_DAG*

// Create indicator for missing information
foreach var of varlist marry* inc* grund* higher* uni* dk* work* married cohab alder* {
gen `var'_miss = 0
replace `var'_miss = 1 if `var' == .    
}
foreach var of varlist inc_m inc_f {
egen mean_`var' = mean(`var')
replace `var' = mean_`var' if `var' == .
drop mean_`var'    
}
foreach var of varlist marry* grund* higher* uni* dk* work* married cohab alder* {
replace `var' = 0 if `var' == .    
}
compress 
save "$work\background", replace 